package org.example.springbirthday.controller;

/*
  Created with IntelliJ IDEA.
  Description:
  User: 九转苍翎
  Date: 2025/8/7
  Time: 15:06
 */
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.springbirthday.constants.Constants;
import org.example.springbirthday.enums.ResultStatus;
import org.example.springbirthday.mapper.BlessInfoMapper;
import org.example.springbirthday.model.BlessInfo;
import org.example.springbirthday.model.Result;
import org.example.springbirthday.service.BlessService;
import org.example.springbirthday.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/bless")
public class BlessController {

    @Autowired
    private BlessService blessService;
    @Autowired
    private BlessInfoMapper blessInfoMapper;

    @RequestMapping("/getBlessInfoList")
    public Result<List<BlessInfo>> getBlessInfoList(Integer page,Integer size){
        log.info("获取留言列表,接收到参数,page:{},size:{}",page,size);
        Result<List<BlessInfo>> result = new Result<>();
        Integer count = blessInfoMapper.count();
        result.setPage(count);
        log.info("祝福语总数,count:{}",count);
        //根据页码和size获取相应的祝福
        List<BlessInfo> blessInfoListByPageAndSize = blessService.getBlessInfoListByPageAndSize((page - 1) * size,size);
        result.setData(blessInfoListByPageAndSize);
        result.setCode(ResultStatus.SUCCESS);
        return result;
    }

    @RequestMapping("/getBlessInfoListByUserId")
    public List<BlessInfo> getBlessInfoListByUserId(HttpServletRequest request){
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        log.info("根据用户id获取留言列表,解析token:{}",token);
        Integer userId = JwtUtils.getIdByToken(token);
        log.info("根据用户id获取留言列表,解析id:{}",userId);
        return blessService.getBlessInfoListByUserId(userId);
    }

    @RequestMapping("/add")
    public Result<String> addBlessInfo(@RequestBody BlessInfo blessInfo, HttpServletRequest request){
        log.info("发布祝福语,blessInfo={}",blessInfo);
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        log.info("发布祝福语,解析token:{}",token);
        Integer userId = JwtUtils.getIdByToken(token);
        log.info("发布祝福语,解析id:{}",userId);
        try {
            Integer ret = blessService.addBlessInfo(blessInfo,userId);
            if(ret == 3){
                return Result.success("祝福语发布成功");
            }
        }catch (Exception e){
            log.error("祝福语发布失败,e",e);
        }
        return Result.fail("祝福语发布失败");
    }

    @RequestMapping("/delete")
    public Result<String> deleteBlessInfo(Integer id,Integer userId){
        log.info("删除祝福语,id:{},userId{}",id,userId);
        Integer ret = blessService.deleteBlessInfo(id,userId);
        if(ret == 3){
            return Result.success("祝福语删除成功");
        }
        return Result.fail("祝福语删除失败");
    }

    @RequestMapping("update")
    public Result<String> updateBlessInfo(@RequestBody BlessInfo blessInfo){
        log.info("updateBlessInfo,blessInfo:{}",blessInfo);
        Integer ret = blessService.updateBlessInfo(blessInfo);
        if(ret == 1){
            return Result.success("祝福语修改成功");
        }
        return Result.fail("祝福语修改失败");
    }
}
